<%@taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@taglib prefix="fn" uri="http://java.sun.com/jsp/jstl/functions"%>
<%@page import="java.util.List"%>
<%@page import="ru.miit.elearning.hibernate.dto.CurriculumLevel"%>

<%--
    Document   : announcements
    Created on : 19.11.2008, 15:33:27
    Author     : Cyclone
--%>

<%@page contentType="text/html" pageEncoding="UTF-8"%>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
    "http://www.w3.org/TR/html4/loose.dtd">

<body>
    <script src="js/announcements.js" type="text/javascript"></script>

    <%@include file="systemMessage.jsp" %>

    <div id="box" class="rounded">
        <div id="rightnow" class="rounded-top">
            <h3 class="reallynow rounded-top">
                <span>Объявления</span>
                <c:if test="${sessionScope.roles.admin || sessionScope.roles.tutor}">
                    <a href="#addMessage" class="add_comment" onclick="document.getElementById('messageSubjectInput').focus();">Добавить объявление</a>
                </c:if>
                <br />
            </h3>
        </div>
        <!--<br/>-->

        <input type ="hidden" name = "idmes" id ="idhid"/>
        <c:choose>
            <c:when test="${!empty announcementList}">
                <c:forEach var="announcement" items="${announcementList}">
                    <div id="rightnow" class="uncorner">
                        <h3 class="reallynow uncorner">
                            <span>${announcement.subject}</span>
                            <c:if test='<%= request.isUserInRole("admin") || request.isUserInRole("tutor")%>'>
                                <a href="#" class="delete_comment" onclick="deleteAnnouncement(${announcement.idAnnouncement});return false;">Удалить объявление</a>
                            </c:if>
                            <br />
                        </h3>


                        <div style="padding: 5px; text-indent: 2px; overflow-y: scroll; max-height: 300px; border-bottom: 1px lightgray dashed;">
                            ${announcement.contentWebLined}
                        </div>
                        <c:choose><c:when test="${!empty announcement.curriculumLevel}">
                                <p>По курсу:
                                    <span style="font-weight: bold">
                                        <a href="Course?id=${announcement.curriculumLevel.id}">${announcement.curriculumLevel.title}</a>
                                    </span>
                                </p>
                            </c:when><c:otherwise>
                                <p>Общее объявление</p>
                            </c:otherwise></c:choose>

                            <p style="float: right;">Дата публикации:
                                <%--<span style="font-weight:bold">${announcement.creationDate.date}.${announcement.creationDate.month+1}.${announcement.creationDate.year+1900}
                                - ${announcement.creationDate.hours}:${announcement.creationDate.minutes}</span>--%>
                                <span>${fn:substring(announcement.creationDate,0,16)}</span>
                        </p>
                        <c:if test="${!empty announcement.account}">
                            <p>Отправитель:
                                <span style="font-weight: bold">
                                    <a href="Profile?id=${announcement.account.id}">${announcement.account.userIdentity.name}</a>
                                </span>
                            </p>
                        </c:if>

                    </div>
                    <br/>

                </c:forEach>
                <form action="DeleteAnnouncement" name="deleteAnnouncementForm" style="display: none;">
                    <input type="hidden" name="announcementId" value="-1" />
                </form>
            </c:when><c:otherwise>
                <p>Список объявлений пуст</p>
            </c:otherwise></c:choose>
 
        <c:if test="${sessionScope.roles.admin || sessionScope.roles.tutor}">
            <form id="form" action="AddAnnouncement" name="addMessageForm" method="post">
                <fieldset class="rounded">
                    <legend class="rounded">
                        <a name="addAnnouncement">Добавление нового объявления</a>
                    </legend>
                    <label for="direction">
                        Направление :
                    </label>
                    <select id="directionSelect" name="directionId" onchange="directionSelectChange();" tabindex="1" >
                        <c:if test="${sessionScope.roles.admin}">
                            <option value="-1">Общее объявление</option>
                        </c:if>
                        
                        <c:forEach var="direction" items="${directionList}">
                            <option value="${direction.id}">${direction.title}</option>
                        </c:forEach>
                    </select>
                    <br/>

                    <!--                <label for="course" style="display: none;" id="courseLabel" tabindex="4">-->
                    <label for="course" id="courseLabel">
                        Курс :
                    </label>
                    <c:if test="${sessionScope.roles.admin}">
                        <input type="text" id="inputChooseCourse" disabled="disabled" value="Общее объявление" tabindex="2" align="left" />
                    </c:if>
                    <%
                                List<CurriculumLevel> directionList = (List<CurriculumLevel>) request.getAttribute("directionList");
                                boolean selectShown = true;
                                for (CurriculumLevel direction : directionList) {

                                    if (selectShown) {
                                        out.print("<script>visibleDivId = " + direction.getId() + ";</script>");
                                    }

                                    out.write("<div id=\"courseSelectDiv_" + direction.getId() + "\" ");
                                    if (!selectShown || request.isUserInRole("admin")) {
                                        out.write(" style=\"display: none;\"");
                                    } else {
                                        selectShown = false;
                                    }
                                    out.write(">");

                                    out.write("<select id=\"courseSelect_" + direction.getId() + "\" tabindex=\"2\" name=\"courseId_" + direction.getId() + "\" onchange=\"courseSelectChange(" + direction.getId() + ");\"");
                                    if (request.isUserInRole("admin")) {
                                        out.write("<option value=\"-1\" selected=\"selected\">Общее объявление</option>");
                                    } else {
                                        out.write("<option value=\"-1\" selected=\"selected\">Выберите курс...</option>");
                                    }
                                    List<CurriculumLevel> courseList = (List<CurriculumLevel>) request.getAttribute("courseList_" + direction.getId());
                                    /* predefined skipped
                                    Integer predefinedCourseId = null;
                                    try {
                                    String predefinedCourseIdString = request.getParameter("predefCourseId");
                                    if (predefinedCourseIdString != null) {
                                    predefinedCourseId = Integer.parseInt(predefinedCourseIdString);
                                    }

                                    } catch (Exception e) {
                                    System.err.println("Smth wrong with predefCourseId");
                                    e.printStackTrace();
                                    }

                                    if (predefinedCourseId == null) {
                                     */
                                    for (CurriculumLevel course : courseList) {
                                        out.write("<option value=\"" + course.getId() + "\">" + course.getTitle() + "</option>");
                                    }
                                    /* predefined skipped
                                    } else {
                                    for (CurriculumLevel course : courseList) {
                                    out.write("<option " + (predefinedCourseId.equals(course.getId()) ? "selected=\"selected\" " : "") + "value=\"" + course.getId() + "\">" + course.getTitle() + "</option>");
                                    }
                                    }
                                     */
                                    out.write("</select>");
                                    out.write("</div>");
                                }
                    %>
                    <label for="subject">
                        Тема объявления :
                    </label>
                    <input name="subject" id="messageSubjectInput" type="text" tabindex="3" />
                    <br/>
                    <label for="content" style="width: 50px;">
                        Текст :
                    </label>
                    <textarea name="content" class="taWysiwyg" tabindex="4" style="width: 88%"></textarea>
                    <br/>
                    <a href="javascript:" class="goWysiwyg" style="margin-left: 58px;">форматировать текст</a>
                    <br/>

                    <input type="hidden" name="type" value="0"/>
                    <input type="hidden" id="courseIdInput" name="courseId" value="-1"/>

                </fieldset>

                <div align="center">
                    <input id="button1" type="button" value="Добавить" tabindex="5" onclick="addMessageFormValidation(${sessionScope.roles.admin});"/>
                    <input id="button2" type="reset" value="Очистить" tabindex="6" />
                </div>
            </form>
        </c:if>
    </div>
    <script type="text/javascript">

        // var defaultVal = 'Введите текст объявления';
        $(document).ready(function(){
            $("#liAnnouncements").addClass("current");
            
            $('div#top-panel p').text('Общие объявления в системе и объявления по Вашим курсам');


        <%--var defaultVal = 'Введите текст объявления';
        // removed id="wysiwig" from textarea. Bring it back to make it work;
        $("#wysiwig").focus(function(){
            if(this.value == defaultVal){
                this.value = '';
            }
        }).blur(function(){
            if(this.value == ''){
                this.value = defaultVal;
            }
        });--%>

            });
    </script>
</body>

